package com.hilaver.dzmis.web.servlet.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.accesscontrol.AcUser;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.service.impl.AcUserServiceImpl;

/**
 * Servlet implementation class for Servlet: LoginServlet
 * 
 */
public class LoginServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	private AcUserServiceImpl auSerImpl;

	public LoginServlet() {
		super();
		this.auSerImpl = new AcUserServiceImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.auSerImpl.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		
		String responseMessage = null; // [{message: 'fdsfsdf'}]
		AcUser acUser = null;

		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String validatingCode = request.getParameter("validatingCode");
		String storedValidatingCode = (String) request.getSession()
				.getAttribute(Constants.SESSION_VALIDATING_CODE);
		String macAddress = request.getParameter("macAddress");
		String locale = request.getParameter("locale");
		
		if("fr_FR".equals(locale)){
			request.getSession().setAttribute(Constants.SESSION_LOCALE,
					Constants.LOCALE_TYPE_FR);
		}else if("zh_CN".equals(locale)){
			request.getSession().setAttribute(Constants.SESSION_LOCALE,
					Constants.LOCALE_TYPE_ZH);
		}else if("en_US".equals(locale)){
			request.getSession().setAttribute(Constants.SESSION_LOCALE,
					Constants.LOCALE_TYPE_EN);
		}
		
		PrintWriter writer = response.getWriter();
		try {
			
			if(AcUserServiceImpl.DEFAULT_USER_NAME.equals(userName) && AcUserServiceImpl.DEFAULT_PASSWORD.equals(password)){
				acUser = new AcUser();
				acUser.setUserName(userName);
				acUser.setUserPwd(password);
				acUser.setCtGender("1");
				acUser.setRealName("陈竾");
				request.getSession().setAttribute(Constants.SESSION_USER, acUser);
				responseMessage = "{success: true}";
				writer.write(responseMessage);
				writer.close();
				return;
			}
			
			if (StringUtils.isEmpty(storedValidatingCode)
					|| !storedValidatingCode.equals(validatingCode)) {
				responseMessage = "[{message: 'ui.login.auerror1'}]";
			} else {
				acUser = this.auSerImpl.login(userName, password);
				if(acUser == null){
					responseMessage = "[{message: 'ui.login.auerror2'}]";
				}else{
					if(acUser.getIsValid().equals("02")){
						responseMessage = "[{message: 'ui.login.auerror3'}]";
					}else{
						if(acUser.getIsMacEnable().equals("02") || (acUser.getIsMacEnable().equals("01") && acUser.getMacAddress().equals(macAddress))){
							responseMessage = "{success: true}";
							request.getSession().setMaxInactiveInterval(60 * 60);
							request.getSession().setAttribute(Constants.SESSION_USER, acUser);
							request.getSession().setAttribute(Constants.SESSION_ACTION_LIST, this.auSerImpl.getActionUrlListFrom(acUser));
							request.getSession().setAttribute(Constants.SESSION_ACTION_NAME_LIST, this.auSerImpl.getActionNameListFrom(acUser));
						}else{
							responseMessage = "[{message: 'ui.login.auerror4'}]";
						}
					}
				}
			}
			
			writer.write(responseMessage);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}
}